

//_______________________________________________________________________________________________________________
/****************************************************************************************************************/
/*                                                                                                              */
/*                     D Y N A M I C   T I M E R S                                                              */
/*                                                                                                              */
/****************************************************************************************************************/

#ifdef OS_ENABLE_DTIMERS

/************************************************************************/
/* Check dynamic timer's states                                         */
/************************************************************************/

#define OS_CheckDTimer(dtimer)      ((dtimer).Flags.bTimeout)       // overflowed
#define OS_IsDTimerActive(dtimer)   ((dtimer).Flags.bActive)        // created
#define OS_IsDTimerStopped(dtimer)  (!((dtimer).Flags.bRun))        // stopped
#define OS_IsDTimerRun(dtimer)      ((dtimer).Flags.bRun)           // run

//------------------------------------------------------------------------------
// Get remaining time of counting
#define OS_GetDTimer(dtimer)      (-(dtimer).Timer)


/************************************************************************/
/* Stop, pause and continue counting                                    */
/************************************************************************/
#define OS_StopDTimer(dtimer)          (dtimer).Flags.bRun = 0        // Stop timer (pause it)
#define OS_PauseDTimer(dtimer)         (dtimer).Flags.bRun = 0        // Stop timer (pause it)
#define OS_ContinueDTimer(dtimer)      (dtimer).Flags.bRun = 1        // Continue after pause

#define OS_BreakDTimer(dtimer)         (dtimer).Flags.bTimeout = 1    // 

/************************************************************************/
/* Services for run dynamic timers                                      */
/************************************************************************/

//------------------------------------------------------------------------------
// Start counting with given time. Timeout flag is cleared.

#define OS_RunDTimer(dtimer, time)          \
    {                                       \
        (dtimer).Flags.bRun = 0;            \
        (dtimer).Timer = -time;             \
        (dtimer).Flags.bTimeout = 0;        \
        (dtimer).Flags.bRun = 1;            \
    }

//------------------------------------------------------------------------------
// Re-run static timer for time time since last overflow. Timeout flag is cleared.

#define OS_UpdateDTimer(dtimer, time)       \
    {                                       \
        (dtimer).Flags.bRun = 0;            \
        (dtimer).Timer -= time;             \
        (dtimer).Flags.bTimeout = 0;        \
        (dtimer).Flags.bRun = 1;            \
    }

//------------------------------------------------------------------------------
// Increase time of dinamic timer counter. Timeout flag remain unchanged.

#define OS_AddDTimer(dtimer, time)          \
    {                                       \
        (dtimer).Flags.bRun = 0;            \
        (dtimer).Timer -= time;             \
        (dtimer).Flags.bRun = 1;            \
    }


/************************************************************************/
/* Wait services                                                        */
/************************************************************************/

//------------------------------------------------------------------------------
// Wait for dynamic timer overflowed

#define OS_WaitDTimer(dtimer)                   \
    {                                           \
        _OS_WAIT_EVENT(OS_CheckDTimer(dtimer)); \
    }


//------------------------------------------------------------------------------
// Delay current task using dynamic timer

#define OS_DelayDTimer(dtimer, delay)       \
    {                                       \
        OS_RunDTimer(dtimer, delay);        \
        OS_WaitDTimer(dtimer);              \
    }









/************************************************************************/
/* REDEFINING OLD NAMES                                                 */
/************************************************************************/

#define OS_CheckPTimer      OS_CheckDTimer
#define OS_IsPTimerActive   OS_IsDTimerActive
#define OS_IsPTimerStopped  OS_IsDTimerStopped
#define OS_IsPTimerRun      OS_IsDTimerRun
#define OS_GetPTimer        OS_GetDTimer
#define OS_StopPTimer       OS_StopDTimer
#define OS_PausePTimer      OS_PauseDTimer
#define OS_ContinuePTimer   OS_ContinueDTimer
#define OS_BreakPTimer      OS_BreakDTimer
#define OS_RunPTimer        OS_RunDTimer
#define OS_UpdatePTimer     OS_UpdateDTimer
#define OS_AddPTimer        OS_AddDTimer
#define OS_WaitPTimer       OS_WaitDTimer
#define OS_DelayPTimer      OS_DelayDTimer
#define OS_CreatePTimer     OS_CreateDTimer
#define OS_DeletePTimer     OS_DeleteDTimer

#define OST_PTIMER          OST_DTIMER

//#define OS_StopDTimer(dtimer)
//#define OS_PauseDTimer(dtimer)
//#define OS_ContinueDTimer(dtimer)
//#define OS_BreakDTimer(dtimer)
//#define OS_RunDTimer(dtimer, time)
//#define OS_UpdateDTimer(dtimer, time)
//#define OS_AddDTimer(dtimer, time)
//#define OS_WaitDTimer(dtimer)
//#define OS_DelayDTimer(dtimer, delay)
//#define OS_CheckDTimer(dtimer)
//#define OS_IsDTimerActive(dtimer)
//#define OS_IsDTimerStopped(dtimer)
//#define OS_IsDTimerRun(dtimer)    
//#define OS_GetDTimer(dtimer)





#endif  // OS_ENABLE_DTIMERS

